#include "tomatoclock.h"
#include "ui_tomatoclock.h"
#include "statistics.h"
#include "wode.h"
#include "taskpage.h"
#include "itemform.h"
#include "achieve.h"
#include "databasemanager.h"
#include "create.h"
#include <QSqlQuery>
#include <QDebug>

tomatoclock *toma;

tomatoclock::tomatoclock(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::tomatoclock)
{
    ui->setupUi(this);
    Achieve g_achieve;
    g_achieve.displayfruit(ui->label_3);

    ui->listWidget->setStyleSheet(
        "QListWidget {"
            "background-color: rgba(255, 255, 255, 128);"
            "color: white;"
        "}"
    );
    ui->listWidget_2->setStyleSheet(
        "QListWidget {"
            "background-color: rgba(255, 255, 255, 128);"
            "color: white;"
        "}"
    );
    ui->listWidget_3->setStyleSheet(
        "QListWidget {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: white;"
        "}"
        );
    ui->listWidget_4->setStyleSheet(
        "QListWidget {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: white;"
        "}"
        );
    ui->start->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->wode->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->task->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->pushButton_4->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->pushButton->setStyleSheet(
        "QPushButton {"
        " background-color: transparent;"
        "color: #91ada2;"
        "}"
        );
    ui->label_3->setStyleSheet(
        "QLabel {"
        "background-color: transparent;"
        "color: #cbd47f;"
        "}"
        );
    toma=this;
    //上半部分：显示最近待办
    display();
}

tomatoclock::~tomatoclock()
{
    delete ui;
}
void tomatoclock::on_pushButton_4_clicked()
{
    Statistics *s=new Statistics;
    this->close();
    s->show();
}//切换到统计
void tomatoclock::on_wode_clicked()
{
    Wode *w=new Wode;
    this->close();
    w->show();
}//切换到设置
void tomatoclock::on_task_clicked()
{
    TaskPage *t1=new TaskPage;
    this->close();
    t1->show();
}//切换到任务集
void tomatoclock::on_pushButton_clicked()
{
    Create *c=new Create;
    c->show();
}

//显示最近几条待办
void tomatoclock::display()
{
    QSqlQuery query(DatabaseManager::getDatabase());
    QString sqlselect = "SELECT * FROM history WHERE user = ? ORDER BY ID DESC LIMIT 3;";
    query.prepare(sqlselect);
    query.addBindValue(DatabaseManager::getUID());

    qDebug()<<"Start display begin";
    if(!query.exec()) {
        qDebug() << "Display: Query execution failed:" << query.lastError().text();
    }
    ui->listWidget->clear();//清空以往数据
    int i=0;
    while(query.next()){
        QString task_name=query.value("task_name").toString();
        QString task_type=query.value("task_type").toString();
        int duration=query.value("duration").toInt();
        QString date=query.value("date").toString();
        //每遍历一条更新ui组件setItemInfo
        QListWidgetItem*item=new QListWidgetItem;
        item->setSizeHint(QSize(650,  120));
        itemform *historyitem=new itemform;
        historyitem->setItemInfo(task_name,task_type,duration,date);
        connect(historyitem, &itemform::itemDataChanged, this, &tomatoclock::display);
        Create *createtemp=new Create;
        connect(createtemp, &Create::itemDataChanged, this, &tomatoclock::display);
        //将每行与数据表连接
        switch (i) {
        case 0:
            ui->listWidget_2->addItem(item);
            ui->listWidget_2->setItemWidget(item,historyitem);
            break;
        case 1:
            ui->listWidget_3->addItem(item);
            ui->listWidget_3->setItemWidget(item,historyitem);
            break;
        case 2:
            ui->listWidget_4->addItem(item);
            ui->listWidget_4->setItemWidget(item,historyitem);
            break;
        default:
            break;
        }
        i++;
    }
}
